package org.tio.examples.httpImpl.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.tio.core.threadpool.DefaultThreadFactory;

public class TImpl {
    
    
    
    public static void main(String[] args) {
    	final HttpImpl http = new HttpImpl();
        LinkedBlockingQueue<Runnable> tioQueue = new LinkedBlockingQueue<Runnable>();
        String tioThreadName = "tio";
        int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2;
        http.setTpe(new ThreadPoolExecutor(CORE_POOL_SIZE, CORE_POOL_SIZE, 90L, TimeUnit.SECONDS, tioQueue, DefaultThreadFactory.getInstance(tioThreadName, Thread.NORM_PRIORITY)));
        http.getTpe().prestartAllCoreThreads();
        try {
            
            http.setChannelGroup(AsynchronousChannelGroup.withThreadPool(http.getTpe()));
            
            http.setServerSocketChannel(AsynchronousServerSocketChannel.open(http.getChannelGroup()));
            
            http.getServerSocketChannel().setOption(StandardSocketOptions.SO_REUSEADDR, true);
            http.getServerSocketChannel().setOption(StandardSocketOptions.SO_RCVBUF, 64 * 1024);
            
            InetSocketAddress listenAddress = new InetSocketAddress(9999);
   
            http.getServerSocketChannel().bind(listenAddress, 0);
            
            http.getServerSocketChannel().accept(http, http.getChImpl());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}
